/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EJERCICIO4;

import EJERCICIO4.Tupla;
import EJERCICIO4.Atributo;
import java.util.ArrayList;
import java.util.List;
import javax.print.DocFlavor;
import java.util.Iterator;
/**
 *
 * @author Cliente
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
        List<Atributo> Cliente = new ArrayList<Atributo>();

            Cliente.add(new Atributo("IdCliente", Integer.class));

            Cliente.add(new Atributo("Nombre",String.class));

            Cliente.add(new Atributo("Direccion", String.class));

            Cliente.add(new Atributo("Telefono", String.class));

            

            Relacion rel = new Relacion(Cliente);
            //tipo de atributos
             List<Comparable> v = new ArrayList<Comparable>();

            v.add(new Integer(1001));

            v.add(new String("Juan"));

            v.add(new String("cusco"));

            v.add(new String("226688"));

            Tupla t = new Tupla(new IdentificadorTupla("Cliente", 0), v);

            GestorTupla man = new GestorTupla(rel, "Cliente");
 
            java.io.RandomAccessFile raf = new java.io.RandomAccessFile("Cliente", "rw");

            System.out.println("Escribir Tupla..." + t);

            byte[] bytes = new byte[1024];

            //man.escribirTupla(t, raf);

            man.escribirTupla(t, bytes, 0);

            raf.close();




            raf = new java.io.RandomAccessFile("Cliente", "r");

            System.out.println("Leer tupla...");

            // t = man.leerTupla(bytes);

            Par<Tupla, Integer> st = man.leerTupla(bytes, 0);

            System.out.println(st);
            //VERIFICAMOS LA LONGITUD DE LA DUPLA
           int CantDupla=GestorTupla.byteSize(rel, t);
                 
           //CONVERTIMOS LA DUPLA A BYTES
               byte[] b =new byte[t.size()];
               for (int i=0;i<t.size();i++)
               {
                byte a=t.asByte(i);
                  b[i]= a;
               }
              //leemos el arreglo de bytes
              System.out.println("Leer bytes...");

            // t = man.leerTupla(bytes);
              for(int i=0;i< b.length;i++)
                System.out.println(b[i]+ "");
           
            raf.close();

        } catch (Exception e) {

            System.err.println("Exception: " + e.getMessage());

            e.printStackTrace(System.err);

        }
    }
}
